package com.pet.serve.controller;

import com.pet.serve.entity.Pets;
import com.pet.serve.repository.PetsRepository;
import com.pet.serve.response.ApiResponse;
import com.pet.serve.service.PetsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/pet")
public class PetsController {

    private final PetsService petsService;
    private final PetsRepository petsRepository;

    @Autowired
    public PetsController(PetsService petsService, PetsRepository petsRepository) {
        this.petsService = petsService;
        this.petsRepository = petsRepository;
    }

    // 分页和条件查询
    @GetMapping("/get")
    public ResponseEntity<ApiResponse> getPets(@RequestParam(defaultValue = "saling") String status,
                                               // 价格区间
                                               @RequestParam(required = false) BigDecimal small,
                                               @RequestParam(required = false) BigDecimal big,
                                               @RequestParam(required = false) String name,
                                               @RequestParam(required = false) String type,
                                               @RequestParam(defaultValue = "1") int page_no,
                                               @RequestParam(defaultValue = "10") int page_size) {

        Pets.PetStatus petStatus = Pets.PetStatus.valueOf(status);
        // 如果 type 为空，说明用户不进行类型过滤
        Pets.PetType petType = null;
        if (type != null && !type.isEmpty()) {
            try {
                petType = Pets.PetType.valueOf(type.toLowerCase());  // 将 type 转为小写后匹配
            } catch (IllegalArgumentException e) {
                return ResponseEntity.badRequest().body(new ApiResponse(false, "Invalid pet type: " + type, null, 0));
            }
        }
        Page<Pets> petsPage = petsService.findPetsByConditionsWithPagination(petStatus, small, big, name,petType, page_no, page_size);
        ApiResponse response = new ApiResponse(true, "Pets retrieved successfully", petsPage.getContent(), petsPage.getTotalElements());
        return ResponseEntity.ok(response);
    }

    @DeleteMapping("/delete")
    public ResponseEntity<ApiResponse> deletePet(@RequestBody List<Long> petids) {
        for (Long petid : petids) {
            boolean result = petsService.deletePetByPetid(petid);
            if (!result) {
                return ResponseEntity.badRequest().body(new ApiResponse(false, "Pet not found", null, 0));
            }
        }
        return ResponseEntity.ok(new ApiResponse(true, "Pet deleted successfully", null, 0));
    }

    @PostMapping("/add")
    public ApiResponse addPet(@RequestBody Pets pet) {
        try {
            petsRepository.save(pet);
            return new ApiResponse(true, "Pet added successfully", null, 0);
        }catch (Exception e) {
            return new ApiResponse(false, "Pet added failed", null, 0);
        }
    }

    @PutMapping("/update")
    public ApiResponse updatePet(@RequestBody Pets pet) {
        try {
            petsRepository.save(pet);
            return new ApiResponse(true, "Pet updated successfully", null, 0);
        }catch (Exception e) {
            return new ApiResponse(false, "Pet updated failed", null, 0);
        }
    }

    // 根据宠物的状态查找宠物
    @GetMapping("/byStatus")
    public ResponseEntity<List<Pets>> findByStatus(@RequestParam Pets.PetStatus status) {
        List<Pets> pets = petsService.findByStatus(status);
        return ResponseEntity.ok(pets);
    }

    // 根据宠物的种类查找宠物
    @GetMapping("/byType")
    public ResponseEntity<ApiResponse> getPetsByType(@RequestParam String type, Pageable pageable) {
        Pets.PetType petType = Pets.PetType.valueOf(type.toLowerCase());  // 可以传入大写的种类，转换为小写，也可以传入小写的种类
        Page<Pets> petsPage = petsService.findByType(petType, pageable);
        ApiResponse response = new ApiResponse(true, "Pets retrieved successfully", petsPage.getContent(), petsPage.getTotalElements());
        return ResponseEntity.ok(response);
    }


    // 根据宠物的价格范围查找宠物
    @GetMapping("/byPriceRange")
    public ResponseEntity<List<Pets>> findByPriceRange(@RequestParam BigDecimal minPrice, @RequestParam BigDecimal maxPrice) {
        List<Pets> pets = petsService.findByPriceRange(minPrice, maxPrice);
        return ResponseEntity.ok(pets);
    }

    // 根据宠物ID查找宠物
    @GetMapping("/byId")
    public ResponseEntity<Pets> findByPetid(@RequestParam Long petid) {
        Pets pet = petsService.findByPetid(petid);
        return ResponseEntity.ok(pet);
    }

    // 根据宠物名字模糊查询
    @GetMapping("/byName")
    public ResponseEntity<List<Pets>> findByNameLike(@RequestParam String name) {
        List<Pets> pets = petsService.findByNameLike(name);
        return ResponseEntity.ok(pets);
    }

    // 根据多个条件查询宠物
    @GetMapping("/byConditions")
    public ResponseEntity<List<Pets>> findPetsByConditions(@RequestParam(required = false) String type,
                                                           @RequestParam(required = false) String status,
                                                           @RequestParam(required = false) Double minPrice,
                                                           @RequestParam(required = false) Double maxPrice) {
        List<Pets> pets = petsService.findPetsByConditions(type, status, minPrice, maxPrice);
        return ResponseEntity.ok(pets);
    }

    // 分页查询宠物
    @GetMapping("/pagination")
    public ResponseEntity<List<Pets>> findPetsWithPagination(@RequestParam int pageNo, @RequestParam int pageSize) {
        List<Pets> pets = petsService.findPetsWithPagination(pageNo, pageSize);
        return ResponseEntity.ok(pets);
    }
}
